package com.example.administrator.myapplication.adapter;


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.mode.RecordData;
import com.example.administrator.myapplication.mode.RecordDataManager;

import java.util.ArrayList;

/**
 * 说明：记录的适配器
 * 作者： 阳2012; 创建于： 2017/5/6 8:52
 */
public class RecordAdapter extends RecyclerView.Adapter<RecordAdapter.MyViewHolder> {
    ArrayList<RecordData> mDatas;
    Context mContext;

    public RecordAdapter(Context context, ArrayList<RecordData> datas) {
        mDatas = datas;
        mContext = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_evaluate_2, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.content.setText(mDatas.get(position).getContent());
        holder.time.setText(mDatas.get(position).getTime());

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MaterialDialog materialDialog = new MaterialDialog.Builder(mContext)
                        .title("提示")
                        .positiveText("确定")
                        .negativeColorRes(R.color.gray)
                        .negativeText("取消")
                        .content("确定要删除该记录吗？")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                RecordDataManager.getInstands().deleteData(mDatas.get(position));
                            }
                        })
                        .build();
                materialDialog.show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView icon;
        TextView content;
        TextView time;
        ImageView delete;

        public MyViewHolder(View view) {
            super(view);
            icon = (ImageView) view.findViewById(R.id.picture);
            content = (TextView) view.findViewById(R.id.content);
            time = (TextView) view.findViewById(R.id.time);
            delete = (ImageView) view.findViewById(R.id.delete);
        }
    }
}
